package com.atguigu.gmall.util;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * 分词工具类
 *
 * @author : ranzlupup
 * @since : 2023/6/4 17:55
 */
public class KeywordUtils {
    public static List<String> splitKeyword(String keyword) throws IOException {
        // 创建集合用于存放切分后的数据
        List<String> result = new ArrayList<>();

        // 创建IK分词对象 ik_smart(true), ik_max_word(false)
        Reader stringReader = new StringReader(keyword);
        IKSegmenter ikSegmenter = new IKSegmenter(stringReader, false);

        // 循环取出切分好的词
        Lexeme next = ikSegmenter.next();
        while (next != null) {
            String word = next.getLexemeText();
            result.add(word);
            next = ikSegmenter.next();
        }

        // 最终返回集合
        return result;
    }

    public static void main(String[] args) throws IOException {
        System.out.println(splitKeyword("你是谁我是谁你好我好大家好"));
    }
}
